# -*- coding: UTF-8 -*-

# 注意，多个except时如果满足第一个，就会执行第一个，下面的不再执行
# except 不存储异常信息
try:
    print(a)
except FileNotFoundError:
    print("a")
except:
    print("cuowu")

# except 可以写多个报错名字，并重新命名，打出报错详细信息
try:
    print(a)

except (NameError,FileNotFoundError) as e:
    print(e)
else:
    print("yes")
# 可以使用父类Exception,BaseException去捕获异常，as命名打印错误详细
# try中只要第一个是错的，第二个正确与否都输出
try:
    open("add")
    print("父类")
except BaseException as e:
    print(e)
# finally无论是否有异常都会输出
finally:
    print("完成")

# 函数嵌套打印异常
# def test1():
#     print("----test1-1----")
#     print(num)
#     print("----test1-2----")
#
#
# def test2():
#     print("----test2-1----")
#     test1()
#     print("----test2-2----")
#
#
# def test3():
#     try:
#         print("----test3-1----")
#         test1()
#         print("----test3-2----")
#     except Exception as result:
#         print("捕获到了异常，信息是:%s"%result)
#
#     print("----test3-2----")
#
#
#
# test3()
# print("------华丽的分割线-----")
# test2()
# 自定义抛出异常
class Caq(Exception):
    def __init__(self,a,b):
        self.a=a
        self.b=b


    def div(self):
        try:
            if self.b == 0:
                raise self.b
        except:
            print("错误，被除数为零")

        else:
            return self.a / self.b
caq = Caq(4,23)
print(caq.div())



# def main():
#     try:
#         a =int(input("请输入除数："))
#         b = int(input("请输入被除数："))
#         if  b == 0 :
#             raise Caq(a,b)
#     except Caq as rel:
#         print("除数不能为：%d" % rel.b)
#     else:
#         print("无异常")
#         print("结果",Caq(a,b).div())
#
#
# main()
# class ShortInputException(Exception):
#     '''自定义的异常类'''
#     def __init__(self, length, atleast):
#         #super().__init__()
#         self.length = length
#         self.atleast = atleast
#
# def main():
#     try:
#         s = input('请输入 --> ')
#         if len(s) < 3:
#             # raise引发一个你定义的异常
#             raise ShortInputException(len(s), 3)
#     except ShortInputException as result:#x这个变量被绑定到了错误的实例
#         print('ShortInputException: 输入的长度是 %d,长度至少应是 %d'% (result.length, result.atleast))
#     else:
#         print('没有异常发生.')
#
# main()